#include "bootpack.h"
#include "package.h"
/*
struct DHCP_DISCOVER{
    unsigned char op;
    unsigned char htype;
    unsigned char hlen;
    unsigned char hops;
    unsigned int xid;
    short secs;
    short flag;
    long ciaddr;
    long yiaddr;
    long siaddr;
    long giaddr;
    unsigned char[16] chaddr;
    unsigned char[64] sname;
    unsigned char[128] file;
    struct DHCP_OPTIONS *options;
};
struct DHCP_OPTIONS{
    unsigned char type;
    unsigned char length;
    unsigned char *value;
};

*/
/*
void init_dhcp_options(struct MEMMAN *memman,struct DHCP_OPTIONS *options){
    //struct DHCP_OPTIONS options_array[7]
    options=(unsigned char *)memman_alloc_4k(memman,64);
        {51,1,{1}},
        {61,7,{1,0x52,0x54,0x00,0x12,0x34,0x56}},
        {50,4,{192,168,1,5},
        {12,15,{'z','m','m'}},
        {60,8,{'r','l','k'}},
        {55,12,{1,25,3,6,44,46,47,31,33,121,249,43}},
        {255,255,{}},
    };
}
*/

void init_discover_package(struct DHCP_DISCOVER * dd){
    dd->op=1;
    dd->htype=1;
    dd->hlen=6;
    dd->hops=0;
    dd->xid=0x83208856;
    dd->secs=0;
    dd->flag=1;
    dd->ciaddr=0;
    dd->yiaddr=0;
    dd->siaddr=0;
    dd->giaddr=0;
    dd->chaddr[5]=0x52;
    dd->chaddr[4]=0x54;
    dd->chaddr[3]=0x00;
    dd->chaddr[2]=0x12;
    dd->chaddr[1]=0x34;
    dd->chaddr[0]=0x56;
    //dd->sname=(unsigned char *)memman_alloc_4k(memman,64);
    //dd->file=(unsigned char *)memman_alloc_4k(memman,128);
    //dd->options=options;
    dd->end=0xffff;
    unsigned char options[44]={
        0x35,0x01,0x01,
        61,7,1,0x52,0x54,0x00,0x12,0x34,0x56,
        50,4,192,168,1,5,
        12,3,'z','m','m',
        60,3,'r','l','k',
        55,12,1,25,3,6,44,46,47,31,33,121,249,43
    };
    int i;
    for(i=0;i<42;i++){
        dd->options[i]=options[i];
    }
}

short calculate_checksum(short* buffer, int bytes)
{
    short checksum = 0;
    unsigned char* end = buffer + bytes;

    // odd bytes add last byte and reset end
    if (bytes % 2 == 1) {
        end = buffer + bytes - 1;
        checksum += (*end) << 8;
    }

    // add words of two bytes, one by one
    while (buffer < end) {
        checksum += buffer[0] << 8;
        checksum += buffer[1];
        buffer += 2;
    }

    // add carry if any
    short carray = checksum >> 16;
    while (carray) {
        checksum = (checksum & 0xffff) + carray;
        carray = checksum >> 16;
    }

    // negate it
    checksum = ~checksum;

    return checksum & 0xffff;
}

void init_ip_package(struct IP_HEADER * header){
    /*
    unsigned char ip_and_headlength;
    unsigned char service_level;
    short length;
    short identify;
    short label_and_offset;
    unsigned char    ttl;
    unsigned char protocal;
    short head_checksum;
    long src_ip;
    long dst_ip;
    struct UDP_PACKAGE udp;
    */
    header->ip_and_headlength=0x45;
    header->service_level=0xff;
    header->length=sizeof(struct IP_PACKAGE);
    header->identify=0x3333;
    header->label_and_offset=0x2<<13;// 不分段
    header->ttl=255;//生存时间设置为最大
    header->protocal=0x17;
    header->head_checksum=0;
    header->src_ip=0;
    header->dest_ip=0xffffffff;
}
void init_mac_header(struct MAC_PACKAGE *mac){
    /*unsigned char dest_mac[6];
    unsigned char src_mac[6];
    short type;
    struct IP_PACKAGE ip;
    */
    unsigned char dest_mac[6]={0,0,0,0,0,0};
    unsigned char src_mac[6]={0x52,0x54,0x00,0x12,0x34,0x56};
    int i;
    for(i=0;i<6;i++){
        mac->dest_mac[i]=dest_mac[i];
        mac->src_mac[i]=GetRegisterValue(1,i+1);
    }
    mac->pre=0xff;
    mac->protocal=0x0800;
}
int show_package(unsigned char *buf_back,struct BOOTINFO *binfo,unsigned char * p,int length,int back_row,char * name,unsigned char * s){
    //unsigned char * p=(unsigned char*)(&(mac_p->ip));
    //sprintf(s,"ip:");
    //unsigned char s[40];
    int i;
    sprintf(s,"[%d]-[%d]-%s",back_row,length,name);
    int length_show=length;
    if(length_show>30)
        length_show=30;
    for(i=0;i<length_show;i++){
        sprintf(s,"%s%02x",s,p[i]);
    }
    sprintf(s,"%s<--->",s);
    for(i=0;i<1;i++){
        sprintf(s,"%s%02x",s,p[length-1-i]);
    }

    putfonts8_asc(buf_back, binfo->scrnx, 50, 350+back_row*16, COL8_FFFFFF, s);
    back_row++;
    return back_row;
}
